/*
	This file is part of "Ants on the web".

    "Ants on the web" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Ants on the web" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Ants on the web".  If not, see <http://www.gnu.org/licenses/>.
*/

package org.utc.ia04.antsontheweb.utilities;

import java.io.Serializable;
import java.io.StringWriter;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.codehaus.jackson.map.ObjectMapper;


public class PheromoneManagerMessage implements Serializable
{
	//serialVersionUID
	private static final long serialVersionUID = 691439236510294194L;
	
	//Attributes
	private HashMap<URL,Double> linksWithPheromone;
	private HashMap<URL,Object> updateUrl;
	
	private URL currentUrl;
	private URL nextUrl;
	
	private boolean currentTarget;
	private boolean nextTarget;
	
	private Set<URL> SetOfUrl;
	private List<URL> resultPath;
	
	
	//Methods -> USE linksWithPheromone OR oldToNewUrl OR source + destination OR listOfUrl
	public PheromoneManagerMessage() {}
	public PheromoneManagerMessage( Set<URL> parSetOfUrl)
	{
		//Constructeur utilise par Ant_Parseur pour passer un set de liens � PhManager
		this.setLinksWithPheromone(null);
		this.setUpdateUrl(null);
		
		this.setCurrentUrl(null);
		this.setNextUrl(null);
		this.resultPath = null;
		
		this.currentTarget = false;
		this.nextTarget = false;
		
		this.SetOfUrl = parSetOfUrl;
	}
	
	public PheromoneManagerMessage( URL parCurrentUrl,boolean parCurentTarget, URL parNextUrl,boolean parNextTarget)
	{
		this.setLinksWithPheromone(null);
		this.setUpdateUrl(null);
		
		this.setCurrentUrl(parCurrentUrl);
		this.setNextUrl(parNextUrl);
		this.resultPath = null;
		
		this.currentTarget = parCurentTarget;
		this.nextTarget = parNextTarget;
		
		this.SetOfUrl = null;
	}
	
	public PheromoneManagerMessage( List<URL> parResultPath)
	{
		this.setLinksWithPheromone(null);
		this.setUpdateUrl(null);
		
		this.setCurrentUrl(null);
		this.setNextUrl(null);
		this.resultPath = parResultPath;
		
		this.currentTarget = false;
		this.nextTarget = false;
		
		this.setSetOfUrl(null);
	}
	
	public PheromoneManagerMessage(HashMap<URL,Double> linksWithPheromone, HashMap<URL, Object> updateUrl, URL source, URL destination, HashSet<URL> listOfUrl)
	{
		this.setLinksWithPheromone(linksWithPheromone);
		this.setUpdateUrl(updateUrl);
		
		this.setCurrentUrl(source);
		this.setNextUrl(destination);
		this.resultPath = null;
		
		this.currentTarget = false;
		this.nextTarget = false;
		
		this.setSetOfUrl(listOfUrl);
	}

	
	
	public void setResultPath(List<URL> resultPath) {
		this.resultPath = resultPath;
	}
	public List<URL> getResultPath() {
		return resultPath;
	}
	public void setLinksWithPheromone(HashMap<URL,Double> linksWithPheromone) {
		this.linksWithPheromone = linksWithPheromone;
	}
	public HashMap<URL,Double> getLinksWithPheromone() {
		return linksWithPheromone;
	}
	public void setUpdateUrl(HashMap<URL,Object> updateUrl) {
		this.updateUrl = updateUrl;
	}
	public HashMap<URL,Object> getUpdateUrl() {
		return updateUrl;
	}
	public void setCurrentUrl(URL currentUrl) {
		this.currentUrl = currentUrl;
	}
	public URL getCurrentUrl() {
		return currentUrl;
	}
	public void setNextUrl(URL nextUrl) {
		this.nextUrl = nextUrl;
	}
	public URL getNextUrl() {
		return nextUrl;
	}
	
		public boolean isCurrentTarget() {
		return currentTarget;
	}
	public void setCurrentTarget(boolean currentTarget) {
		this.currentTarget = currentTarget;
	}
	public boolean isNextTarget() {
		return nextTarget;
	}
	public void setNextTarget(boolean nextTarget) {
		this.nextTarget = nextTarget;
	}
	public void setSetOfUrl(Set<URL> setOfUrl) {
		SetOfUrl = setOfUrl;
	}
	public Set<URL> getSetOfUrl() {
		return SetOfUrl;
	}

	//Serialization/Deserialization
	public static PheromoneManagerMessage deserialize_withException(String s) throws RequeteException
	{
		ObjectMapper m = new ObjectMapper();
		try
		{
			PheromoneManagerMessage q = m.readValue(s, PheromoneManagerMessage.class);
			return q;
		}
		catch (Exception ex)
			{ throw new RequeteException("Erreur de deserialisation"); }
	}
	
	public String serialize()
	{
		ObjectMapper m = new ObjectMapper();
		StringWriter sw = new StringWriter();
		try{
			m.writeValue(sw, this);
			return sw.toString();
		}
		catch(Exception ex){
			//System.out.println("Erreur de serialization");
			return "";
		}
	}

//END OF CLASS
}